package com.ra.landlord;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.jdbc.ReturningWork;
import org.springframework.stereotype.Service;

import com.espirit.eap.sa.Privilege;
import com.googlecode.cswish.struts.spring.GenericService;

@Service
public class LandlordAssistUserService {

	@Resource
	private GenericService genericService;
	
	public List<Privilege> convertVcPermission(final LandlordAssistUser landlordAssistUser) {
		
		List<Privilege> privilegeList = genericService.doReturningWork(new ReturningWork<List<Privilege>>() {

			@Override
			public List<Privilege> execute(Connection connection)
					throws SQLException {
				String sql = "select relation_perm, relation_perm_ext from ra_building_landlorduser where user_id=? and data_status=1";
				
				List<Privilege> privilegeList = new ArrayList<>();
				try (PreparedStatement ps = connection.prepareStatement(sql)) {
					ps.setInt(1, landlordAssistUser.getId());
					
					try (ResultSet rs = ps.executeQuery()) {
						if (rs.next()) {
							long perm = rs.getLong(1);
							long permExt = rs.getLong(2);
							
							convertPermToPrivilegeList(perm, permExt, privilegeList);
						}
					}
				}
				return privilegeList;
			}

			private void convertPermToPrivilegeList(long perm, long permExt, List<Privilege> privilegeList) {
				// TODO Auto-generated method stub
				
			}
		});
		
		return privilegeList;
	}
}
